package com.antoco.controlcenter.comm;

import com.antoco.controlcenter.comm.commLink.CommLinkTcp;
import com.antoco.controlcenter.comm.message.NciMessage;
import com.antoco.controlcenter.iface.IMoveEventListener;
import com.antoco.controlcenter.utils.BytesUtils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;

public class RobotHandler {
    private String commString;
    private CommLinkTcp mCommLink; //通信组件
    public PropertyManager propertyManager;
    private ConcurrentMap<String, String> instructMap = new ConcurrentHashMap<>(); //指令表
    private ConcurrentLinkedQueue<NciMessage> nciQueue = new ConcurrentLinkedQueue<>(); //NCI指令队列

    private DistanceTcpClient distanceClient;

    public RobotHandler() {
        propertyManager = new PropertyManager();
    }

    public void setCommString(String param) {
        this.commString = param;
    }

    public void startCommunication() {
        mCommLink = new CommLinkTcp();
        mCommLink.setCommEventListener(message -> propertyManager.parserMessage(message));

        mCommLink.connect(commString);
        //绕线机构数据返回
        distanceClient = new DistanceTcpClient();
        distanceClient.setOnDataReceiveListener(message -> {
            propertyManager.setPropertieNumberValue("distance", (short) BytesUtils.lowBytesToInt((byte[]) message, 6, 2) / 100);
            propertyManager.setPropertieNumberValue("battery", (short) BytesUtils.lowBytesToInt((byte[]) message, 4, 2) / 100);
        });
        distanceClient.connect("192.168.1.101", 23);

    }


    public int getNumberValue() {
        int robotGear = 0;
        PropertyBase propertyGear = propertyManager.getProperty("gear");
        if (propertyGear != null && propertyGear.getValue() != null) {
            robotGear = (int) propertyGear.getNumberValue();
        }
        return robotGear;
    }

    /**
     * 注销机器人控制器
     */
    public void dispose() {

    }

    //TODO: 获取通信状态

    public void addNci(NciMessage nci) {
        nciQueue.add(nci);
    }

    public void addInstruct(String name, String nci) {
        this.instructMap.put(name, nci);
    }


    public void clearInstruct() {
        this.instructMap.clear();
    }

    public ConcurrentMap<String, String> getInstructMap() {
        return instructMap;
    }


    public ConcurrentLinkedQueue<NciMessage> getNciQueue() {
        return nciQueue;
    }

    public IMoveEventListener getMoveEventListener() {
        return mCommLink.getMoveEventListener();
    }

    public void stopCommunication() {
        //thread_flag = false;
        if (mCommLink != null) {
            mCommLink.disConnect();
        }
        if (distanceClient != null) {
            distanceClient.disconnect();
        }
    }

    public void destory() {
        stopCommunication();
        distanceClient.disconnect();
    }

    public void clearConfig() {
        setCommString("");
        propertyManager.clear();
        clearInstruct();
    }
}
